<script setup>
import { reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../store/auth'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'

const router = useRouter()
const auth = useAuthStore()
const data = reactive({ username: '', password: '' })

const handleAdminLogin = async () => {
  try {
    if (!data.username || !data.password) {
      ElMessage.warning('用户名和密码不能为空')
      return
    }
    const response = await fetch('http://127.0.0.1:8000/login', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ username: data.username, password: data.password })
    })
    const result = await response.json()
    if (response.ok) {
      if (result.user_type !== 'admin') {
        ElMessage.error('仅管理员可登录')
        return
      }
      auth.setLoginData({ token: result.token, role: result.user_type, userId: result.user_id })
      ElMessage.success('登录成功')
      router.push('/admin')
    } else {
      ElMessage.error(result.detail || '登录失败')
    }
  } catch (err) {
    ElMessage.error('网络错误')
  }
}
</script>

<template>
  <div class="login-container">
    <div class="login-background"></div>
    <el-card class="login-card">
      <div class="logo-container">
        <img src="../assets/images/logo.png" alt="管理员登录" class="logo-image"/>
      </div>
      <h2 class="login-title">管理员登录</h2>
      <div class="input-group">
        <el-input v-model="data.username" placeholder="请输入用户名" clearable>
          <template #prefix>
            <el-icon><User /></el-icon>
          </template>
        </el-input>
      </div>
      <div class="input-group">
        <el-input v-model="data.password" type="password" placeholder="请输入密码" show-password>
          <template #prefix>
            <el-icon><Lock /></el-icon>
          </template>
        </el-input>
      </div>
      <div class="button-group">
        <el-button type="primary" class="action-btn" @click="handleAdminLogin">登录</el-button>
      </div>
      <div class="login-footer">
        <el-button type="text" @click="router.push('/')">返回主页</el-button>
      </div>
    </el-card>
  </div>
</template>

<style scoped lang="scss">
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  position: relative;
  overflow: hidden;
}

.login-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: #f5f7fa;
  z-index: -1;
}

.login-card {
  width: 380px;
  padding: 30px;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  background-color: #ffffff;
  z-index: 1;
}

.logo-container {
  display: flex;
  justify-content: center;
  margin-bottom: 15px;
}

.logo-image {
  width: 80px;
  height: auto;
}

.login-title {
  text-align: center;
  margin-bottom: 24px;
  color: #333;
  font-weight: 600;
}

.input-group {
  margin-bottom: 20px;
}

.button-group {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.login-footer {
  display: flex;
  justify-content: center;
  margin-top: 10px;
}

.action-btn {
  width: 100%;
  height: 40px;
  font-size: 16px;
}
</style> 